Skip to content

Walk sets of states instead of determinizing, add symmetric_difference - #28

Merged
alexvbrdn merged 4 commits into
mainfrom
avoid-determinization
Nov 20, 2026
Merged

alexvbrdn merged 4 commits into
mainfrom
avoid-determinization

Conversation

@alexvbrdn

@alexvbrdn alexvbrdn commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

What this changes

Set operations without determinization

equivalent, subset, cardinality and difference no longer determinize or complement their operands. Instead they work on the sets of states a word can reach in each automaton, and only build the sets that are actually reached.

  • equivalent: bisimulation up to congruence (HKC, Bonchi & Pous, POPL 2013). Two deterministic automata are compared state by state with Hopcroft & Karp (1971).
  • subset: antichains (De Wulf, Doyen, Henzinger & Raskin, CAV 2006).
  • cardinality: on a non-deterministic automaton, strings are counted per set of states, so a string with several paths is counted once. It returns BigInteger as soon as one set's count overflows.
  • difference: each state of the result pairs a state of the minuend with the set of subtrahend states the same word reaches. States that can't reach an accept state are left out as the result is built.
  • has_intersection no longer builds the intersection. Term::is_finite looks for a cycle instead of counting. Term::is_total uses subset. remove_state / remove_states no longer scan every state's transitions.

Behaviour change: when the execution profile disables implicit determinization, FastAutomaton::equivalent, subset, cardinality and difference now accept non-deterministic automata instead of returning DeterministicAutomatonRequired. Only minimize and complement still determinize implicitly.

New: symmetric difference

  • FastAutomaton::symmetric_difference: the strings exactly one of two automata accepts. Neither operand is determinized, and the result is deterministic when both are.
  • FastAutomaton::symmetric_difference_all / symmetric_difference_all_par, and Term::symmetric_difference: the strings an odd number of the automata accept. The automata are walked together rather than two by two. _all_par needs the parallel feature and is absent on wasm.

Fixes

  • Regex concatenation is linear. Parsing, RegularExpression::concat_all, simplify and Term::concat used to copy everything accumulated so far for each expression added. A pattern of 5,000 groups now parses in 10 ms instead of 1.3 s, and the parser checks the timeout as it builds a concatenation.
  • Term::union respects the timeout for terms backed by regular expressions.
  • RegularExpression::is_empty / is_empty_string are exact on expressions that aren't simplified (for example ()* parsed without simplification), and so are the matching Term methods.
  • length returns a maximum of 0 for an unbounded repetition of the empty string (()*, ()+, (){2,}).
  • subset no longer overflows the stack on very large sets of states (SetTrie::has_subset_of is iterative).

Internal

  • The degenerate-case wrapper around the union state count moved from the impl into union.rs's tests module.
  • The public API documentation is rewritten, and symmetric_difference is added to the README's method table.

Performance

On the new benches in benches/operations.rs:

  • equivalent / subset: 2 to 20 times faster, and about 100 times faster where most pairs are skipped.
  • difference: up to 3 times faster, and about 170 times faster when the minuend reaches few of the subtrahend's sets.
  • has_intersection: about 3 times faster.
  • minimize: 17% faster on the large pattern.

Checklist

  • cargo fmt --all --check and cargo clippy --all-targets -- -D warnings pass
  • cargo test and cargo test --no-default-features pass
  • New public items are documented
  • A test covers the change
  • CHANGELOG.md is updated under ## [Unreleased]
  • The change builds on Rust 1.88, or the MSRV bump is intentional and noted

@alexvbrdn alexvbrdn self-assigned this Nov 19, 2026
@alexvbrdn
alexvbrdn force-pushed the avoid-determinization branch from 2c16da9 to ea0e939 Compare November 19, 2026 18:21
equivalent, subset, cardinality and difference no longer determinize or
complement their operands: they walk the sets of states a word can reach
(bisimulation up to congruence for equivalent, antichains for subset).
Two deterministic automata are compared with Hopcroft & Karp.

has_intersection no longer builds the intersection, Term::is_finite looks
for a cycle instead of counting, Term::is_total uses subset, and
remove_state(s) no longer scan every state's transitions.
Since #11, `union_mut_nondegenerate` checks the state limit with
`union_state_count_nondegenerate`, and only `test_heuristic` used
`union_state_count_heuristic`, which added the degenerate cases and was
kept in the `impl` behind `#[cfg(test)]`. It is now a helper of the
tests module, and the remaining count is `union_state_count`.
`FastAutomaton::symmetric_difference`, `symmetric_difference_all` and
`symmetric_difference_all_par`, and `Term::symmetric_difference`, built
like `difference` without determinizing an operand. `difference` leaves
out the pairs that reach no accept state as it builds its result.

`SetTrie::has_subset_of` no longer recurses per state of a stored set,
which overflowed the stack of a thread in `subset`.

Regular expressions: concatenation folds in place, in time linear in the
number of expressions, and parsing checks the timeout as it concatenates;
`Term::union` of regular expressions respects the timeout;
`is_empty`, `is_empty_string` and `length` are exact on expressions that
are not simplified.

Rewrites the documentation of the public API.
@alexvbrdn
alexvbrdn force-pushed the avoid-determinization branch from 5dc3db4 to d17c4c4 Compare November 20, 2026 19:19
@alexvbrdn alexvbrdn changed the title Walk sets of states instead of determinizing Walk sets of states instead of determinizing, add symmetric_difference Nov 20, 2026
@alexvbrdn
alexvbrdn merged commit d5a310d into main Nov 20, 2026
9 checks passed
@alexvbrdn
alexvbrdn deleted the avoid-determinization branch November 20, 2026 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant